package com.nengchuan.eic.nceconomy.component.excel;

import com.nengchuan.eic.nceconomy.constant.CommonConstant;
import com.nengchuan.eic.nceconomy.constant.TemplateNameEnum;
import com.nengchuan.eic.nceconomy.dao.TemplateSheetMapper;
import com.nengchuan.eic.nceconomy.entity.po.ExcelSheet;
import com.nengchuan.eic.nceconomy.entity.po.VerifyResult;
import com.nengchuan.util.NullCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *概算变电站增值税模板处理
 */
public class RoughCalculationSubstationAvxHandler extends BaseHandler {

    //以下是需要验证的模板页
    private final static String[] SHEET_NAME_PREFIX={"变电总算表","安装部分汇总表","建筑部分汇总表","建筑单位工程汇总表","安装单位工程汇总表","其他费用表","建设场地征用及清理费用表"};

    private final static String[] SHEET_NAME_SUFFIX={"表一甲","表二甲","表二乙","表三乙","表三甲","表四","表七"};

    private final static int[] FIELD_IDX_END={2,3,3,3,3,2,2};

    private final static int[] LINE_INDEX_IDX={1,1,1,2,2,1,1};

    private final static int[] DATA_IDX_START={2,2,2,4,4,4,4};

    @Qualifier("excelHandlerSupport")
    @Autowired
    private HandlerSupport handlerSupport;

    @Autowired
    private TemplateSheetMapper templateSheetMapper;

    @Override
    public ResponseModel handlerRequest(RequestModel request) {
        String fileName=request.getTemplateName();

        List<ExcelModel> excelModels=new ArrayList<>(SHEET_NAME_PREFIX.length);
        //如果文件名称包含关键字,则由本对象处理
        if (fileName.contains(TemplateNameEnum.ROUGH_CALCULATION_SUBSTATION_ADDED_VALUE_TAX.text)) {
            Map<String,List<String[]>> data=request.getData();

            //进行强制校验 1.校验表名是否存在  2.校验值的第一列第一行是否为空
            List<VerifyResult> verifyResults=handlerSupport.forceVerify(data,SHEET_NAME_PREFIX,SHEET_NAME_SUFFIX,FIELD_IDX_END,DATA_IDX_START);
            for (VerifyResult verifyResult : verifyResults) {
                if(verifyResult.getStatus()== CommonConstant.verifyResult.STATUS_FAIL){
                    return ResponseModel.builder().verifyResults(verifyResults).build();
                }
            }

            //遍历所有集合,key为sheetName value为sheet中的数据集合
            int order=0;
            for (Map.Entry<String, List<String[]>> e : data.entrySet()) {
                String sheetName = e.getKey();
                List<String[]> metadata = e.getValue();

                if(NullCheckUtils.isNull(sheetName)){
                    continue;
                }

                //下面循环为需要解析的sheet页
                ExcelModel response=new ExcelModel();
                Long templateSheetId;
                for (int i = 0; i <SHEET_NAME_PREFIX.length ; i++) {
                    if (sheetName.contains(SHEET_NAME_PREFIX[i]) && sheetName.contains(SHEET_NAME_SUFFIX[i])) {
                        //根据sheetName模糊匹配模板sheetId,并解析创建sheet对象
                        templateSheetId=templateSheetMapper.getBySheetName(SHEET_NAME_PREFIX[i]);
                        ExcelSheet excelSheet = handlerSupport.createExcelSheet(templateSheetId,sheetName,order++);
                        response.setExcelSheet(excelSheet);

                        request.setSheetName(sheetName);
                        handlerSupport.createExcelDic(metadata, response,request,FIELD_IDX_END[i],LINE_INDEX_IDX[i],DATA_IDX_START[i],verifyResults);

                        excelModels.add(response);
                    }
                }
            }
            return ResponseModel.builder().isStorage(true).verifyResults(verifyResults).excelModels(excelModels).build();
        }
        //如果下级处理为空,则相应处理失败
        if(this.nextHandler==null){
            return null;
        }

        //交给下一级节点处理
        return this.nextHandler.handlerRequest(request);
    }
}
